<?php
/**
 * Created by PhpStorm.
 * User: iraklis
 * Date: 1/19/14
 * Time: 2:10 AM
 */
include 'config.php';
include 'db_connection.php';

$day = getdate();
$alert = $day['year'] . '-' . $day['mon'] . '-' . $day['mday'];

$query = mysql_query("SELECT * FROM USERS;");

while ($users = mysql_fetch_assoc($query)) {
    $u_id = $users['u_id'];
    $mail = $users['email'];
    $query2 = mysql_query("SELECT * FROM REMINDERS_RECURRANCY WHERE DATE(start) = '$alert' AND u_id = '$u_id';");

    while($reminders_rec = mysql_fetch_assoc($query2)){
        $parent_id = $reminders_rec['parent_id'];

        // extracting the time from the datetime field start from REMINDERS_RECURRANCY
        $send = strtotime($reminders_rec['start']);

        $current_date = $day[0];
        $query3 = mysql_query("SELECT * FROM REMINDERS WHERE parent_id = '$parent_id' AND u_id = '$u_id';");
        $reminders = mysql_fetch_assoc($query3);

        // converting offset hours to seconds to timestamp
        $offset_send_reminder = $send  - ($reminders['send_reminder']*3600);
echo '<br>' . $current_date . ' - ' . $offset_send_reminder . '<br>';
        echo '<br>' . ($offset_send_reminder - $current_date);
        //echo '<br>' . gettype($offset_send_reminder);

        // checks if current time equals the determined time of the send reminder
        if ($current_date == $offset_send_reminder) {
            $subject = $reminders_rec['title'];
            $message = $reminders['description'];
            $location = $reminders['location'];
            $to = $mail;
            $from = "RemindMe";
           //echo $to . ' - ' . $from . ' - ' . $message . '-' . $location . '<br>';
           //mail($to, "Subject: $subject", $message, "From: $from" );
        }// end of if
    }

}// end of while
